home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / DSUTIL12 / ASCZ2BIN / ASCZ2ASM.PAS < prev   
Pascal/Delphi Source File  |  1993-10-28  |  18KB  |  570 lines

  1. {-----------------------------------------------------------------------}
  2. { PROJECT        NON-PROFIT HIGH QUALITY PROFESSIONAL SOFTWARE,  }
  3. {            AVAILABLE FOR ALL WORLD                }
  4. { LIBRARY        SYSTEM UTILITIES                                }
  5. { MODULE        ASCIIZ_TO_ASM_CONVERTER                         }
  6. { FILE NAME        ASCZ2ASM.PAS                    }
  7. { PURPOSE        Convert the binary file                         }
  8. {                       (ASCII MESSAGES TERMINATED BY NULL)             }
  9. {                       to an assembler format file.                    }
  10. { VERSION        1.10                        }
  11. { DATE            28-Oct-93                    }
  12. { DESIGN        Dmitry Stefankov                }
  13. { IMPLEMENTATION    Dmitry Stefankov                 }
  14. { COMPANY        Freelance Software Engineer            }
  15. { ADDRESS        Isakowskogo str, 4-2-30                }
  16. {            Moscow, 123181                    }
  17. {            USSR                        }
  18. {            Tel. 007 (095) 944-6304                }
  19. { COPYRIGHT NOTICE    Copyright (C) 1987-1993, Dmitry Stefankov    }
  20. { RESTRICTED RIGHTS    AVAILABLE ONLY FOR FREE DISTRIBUTION,           }
  21. {            NOT FOR COMMERCIAL PURPOSE            }
  22. { COMPUTER        IBM PC or compatible                }
  23. { OPERATING SYSTEM    MS/PC-DOS Version 3.30 or higher        }
  24. { COMPILER        Turbo Pascal Version 6.0            }
  25. {                       (Borland International Inc.)  or compatible     }
  26. { ASSEMBLY LANGUAGE    Microsoft MASM 5.10 or compatible               }
  27. { LINKER        Turbo Pascal internal                           }
  28. { ARGUMENTS        <infile>     -   input  stream                  }
  29. {                       <outfile>    -   output stream                  }
  30. {                       <maxbytes>   -   max # of chars per one line    }
  31. {                       <dispofs>    -   display file offset of string  }
  32. { RETURN        None                        }
  33. { REQUIRES        None                                            }
  34. { NATURAL LANGUAGE      English Language                                 }
  35. { SPECIAL        None                        }
  36. { DESCRIPTION        1.Read from input stream                        }
  37. {                       2.Write ASM-format  output stream               }
  38. { REVISION HISTORY    Dima Stefankov (DS)                }
  39. {               1.00   13-Jul-93  DS  initilal release        }
  40. {            1.01   22-Sep-93  DS  some style updates    }
  41. {            1.10   28-Oct-93  DS  some minor changes    }
  42. {-----------------------------------------------------------------------}
  43.  
  44.  
  45. {*======================= PROGRAM HEADER PART ==========================*}
  46.  
  47. PROGRAM   AsciizFileToAssemblerFormatFile;
  48.  
  49.  
  50. {*** other modules ***}
  51. {*USES;*}
  52.  
  53.  
  54. {** switches for compilation **}
  55. {$S-}                {*  stack checking  *}
  56. {$R-}                   {*  range checking  *}
  57.  
  58.  
  59. {*========================== CONSTANTS PART ============================*}
  60.  
  61. CONST
  62.      asPurpose                  =       'AsciizAsmFile Converter';
  63.      asVersion                  =       '1.10';
  64.      asAuthor                   =       'Dima Stefankov';
  65.      asCopyright                =       'Copyright (c) 1987, 1993';
  66.      asProgram            =    'Ascz2asm';
  67.      asProgramPrompt            =       asProgram+': ';
  68.      asProgramU            =    'ASCZ2ASM';
  69.  
  70.      { exit codes }
  71.      errTerminateOK             =     0;
  72.      errBadParmsNumber          =     1;
  73.      errSourceNotFound          =     2;
  74.      errDestDontWrite           =     3;
  75.      errSameNames               =     4;
  76.      errSrcOpenFailed           =     6;
  77.      errDestCreateFailed        =     7;
  78.      errBadBytesValue           =     8;
  79.  
  80.    { miscellaneous }
  81.      aPercent100                =     100;
  82.      achHexPrefix               =     '$';
  83.      achDosExtMark              =     '.';
  84.      asBlankStr                 =     '';
  85.      asSpaces2                  =     '  ';
  86.      asSpaces5                  =     '     ';
  87.      asInDefExt                 =     'bin';
  88.      asOutDefExt                =     'asm';
  89.  
  90.    { defaults }
  91.      aDefBytesPerLine           =     64;
  92.      aMaxBytesPerLine           =     128;
  93.      aMaxOnHeap                 =     65520;
  94.  
  95.    { characters }
  96.      achNULL                    =     #0;
  97.      achHTAB                    =     #9;
  98.      achCR                      =     #13;
  99.      achSPACE                   =     ' ';
  100.      achZero                    =     '0';
  101.      achSkip                    =     '.';
  102.      achDelimiter               =     ',';
  103.      achStrDelim                =     '''';
  104.      achAnotherStrDelim         =     '"';
  105.      achSemicolon               =     ';';
  106.  
  107.    { user confirm }
  108.      achYes                     =     'Y';
  109.      achNo                      =     'N';
  110.  
  111.    { assembler directives and equates }
  112.      achHexSuffix               =     'h';
  113.      asAsmDataDefine            =     'DB';
  114.      asAsmNoList                =     '.XLIST';
  115.      asAsmList                  =     '.LIST';
  116.  
  117.  
  118. {*==================== TYPE DECLARATIONS PART ==========================*}
  119.  
  120. TYPE
  121.     STR2        =       STRING[2];
  122.     STR3        =       STRING[3];
  123.     STR4        =       STRING[4];
  124.     STR8        =       STRING[8];
  125.     STR80       =       STRING[80];
  126.  
  127.  
  128. {*====================== TYPED CONSTANTS PART ==========================*}
  129.  
  130. CONST
  131.  
  132.     setHexChars  :    SET OF System.Char  =  ['0'..'9','A'..'F','a'..'f'];
  133.     setAscii7    :    SET OF System.Char  =  [#32..#126];
  134.     setAscii8    :    SET OF System.Char  =  [#32..#126,#128..#255];
  135.  
  136.  
  137.     gabDisplayStrOffset  :       System.Boolean   =   System.False;
  138.     gadbSymbolsInString  :       System.Byte      =   aDefBytesPerLine;
  139.     gddStrInFileOfs      :       System.Longint   =   0;
  140.     gddReadBytesCount    :       System.Longint   =   0;
  141.     gasOutAsmLine        :       STRING           =   asBlankStr;
  142.  
  143.  
  144. {*=========================== VARIABLES PART ===========================*}
  145.  
  146. VAR
  147.    gfInputByteStream    :       FILE  OF  System.Byte;
  148.    gfInputStream        :       FILE ABSOLUTE gfInputByteStream;
  149.    gsInFileName         :       STR80;
  150.  
  151.    gfOutputFormatText   :       System.Text;
  152.    gfOutputStream       :       FILE  ABSOLUTE  gfOutputFormatText;
  153.    gsOutFileName        :       STR80;
  154.  
  155.    gddByteCount         :       System.Longint;
  156.    gddInFileSize        :       System.Longint;
  157.  
  158.    giErrorCode          :       System.Integer;
  159.    gsTempInput          :       STRING;
  160.    gchInput             :       System.Char;
  161.  
  162.  
  163.  
  164. {*=========================== FUNCTIONAL PART ==========================*}
  165.  
  166. FUNCTION  _fnbFileExist(VAR fStruc : FILE;
  167.                             sFileName : STRING) : System.Boolean;
  168. {* Check that file exits. *}
  169. VAR
  170.   bResult  :  System.Boolean;
  171.  
  172. BEGIN
  173.   {** attempt to open the file **}
  174.     System.Assign(fStruc,sFileName);
  175.     {$I-}
  176.     System.Reset(fStruc);
  177.     {$I+}
  178.  
  179.   {** copy the result of last I/O operation **}
  180.     bResult := (System.IOResult = 0);
  181.  
  182.     IF (bResult)
  183.       THEN  System.Close(fStruc);
  184.     {if-then}
  185.  
  186.   _fnbFileExist := bResult;
  187. END; { _fnbFileExist }
  188.  
  189.  
  190. FUNCTION  _fnsForceFileNameExt(sFileName, sDefExt : STRING) : STRING;
  191. {* Add extension for filename if not present. *}
  192. BEGIN
  193.    IF (System.Pos(achDosExtMark,sFileName) = 0)
  194.      THEN  sFileName := sFileName + achDosExtMark + sDefExt;
  195.    {if-then}
  196.   _fnsForceFileNameExt := sFileName;
  197. END;
  198. { _fnsForceFileNameExt }
  199.  
  200.  
  201. FUNCTION   _fnchGetFirstChar(sInput : STRING) : System.Char;
  202. {* Returns a first char from string. *}
  203. VAR
  204.   chTemp  :  System.Char;
  205.  
  206. BEGIN
  207.    IF (System.Length(sInput) <> 0)
  208.      THEN  chTemp := sInput[1]
  209.      ELSE  chTemp := achNULL;
  210.    {if-then-else}
  211.   _fnchGetFirstChar := chTemp;
  212. END;
  213. { _fnchGetFirstChar }
  214.  
  215.  
  216. FUNCTION   _fnsByteToHexFmt(dbInput : System.Byte) : STR2;
  217. {* Converts a byte to the hex format number representation. *}
  218. CONST
  219.     dbHexCharTable : ARRAY[0..15] OF System.Char = '0123456789ABCDEF';
  220.  
  221. BEGIN
  222.   _fnsByteToHexFmt := dbHexCharTable[dbInput SHR 4] +
  223.                       dbHexCharTable[dbInput AND $0F];
  224. END;  { _fnsByteToHexFmt }
  225.  
  226.  
  227. FUNCTION   _fnsWordToHexFmt(dwInput : System.Word) : STR4;
  228. {* Converts a word to the hex format number representation. *}
  229. BEGIN
  230.   _fnsWordToHexFmt := _fnsByteToHexFmt(System.Hi(dwInput)) +
  231.                       _fnsByteToHexFmt(System.Lo(dwInput));
  232. END;  { _fnsWordToHexFmt }
  233.  
  234.  
  235. FUNCTION   _fnsDoubleWordToHexFmt(ddInput : System.Longint) : STR8;
  236. {* Converts a double word to the hex format number representation. *}
  237. BEGIN
  238.   _fnsDoubleWordToHexFmt := _fnsWordToHexFmt(System.Word(ddInput SHR 16)) +
  239.                       _fnsWordToHexFmt(System.Word(ddInput and $0000FFFF));
  240. END;  { _fnsDoubleWordToHexFmt }
  241.  
  242.  
  243. FUNCTION   _fnsByteToAsmFormat(dbInput : System.Byte) : STR4;
  244. {* Converts a byte to the ASM format number representation. *}
  245. BEGIN
  246.   _fnsByteToAsmFormat := achZero +
  247.                          _fnsByteToHexFmt(dbInput) +
  248.                          achHexSuffix;
  249. END;  { _fnsByteToAsmFormat }
  250.  
  251.  
  252. FUNCTION  _fnsUpcaseStr(sInput : STRING) : STRING;
  253. {* Make all uppercase. *}
  254. VAR
  255.   dbIndex  :  System.BYTE;
  256.   dbCount  :  System.BYTE;
  257.  
  258. BEGIN
  259.   dbCount := System.Length(sInput);
  260.  
  261.   IF (dbCount <> 0)  THEN
  262.     FOR  dbIndex :=  1  TO  dbCount DO
  263.         sInput[dbIndex] := System.Upcase(sInput[dbIndex]);
  264.     {for-to-do}
  265.   {if-then}
  266.  
  267.    _fnsUpcaseStr := sInput;
  268. END; { _fnsUpcaseStr }
  269.  
  270.  
  271. FUNCTION  _fnsAsmPreformat : STRING;
  272. {* Do an assembler DB directive. *}
  273. BEGIN
  274.       _fnsAsmPreformat :=  achHTAB +
  275.                            achHTAB +
  276.                            asAsmDataDefine +
  277.                            achSPACE +
  278.                            achSPACE +
  279.                            achSPACE;
  280. END;
  281. { _fnsAsmPreformat }
  282.  
  283.  
  284. FUNCTION  _fnsAddHTabs(sInput : STRING) : STRING;
  285. {* Adds # of htabs, based on the string length. *}
  286. VAR
  287.   dbLength   :   System.Byte;
  288.  
  289. BEGIN
  290.    dbLength := 4;
  291.    IF (System.Length(sInput) >= 7)
  292.      THEN  System.Dec(dbLength);
  293.    IF (System.Length(sInput) >= 14)
  294.      THEN  System.Dec(dbLength);
  295.    FillChar(sInput[1],dbLength,achHTAB);
  296.    sInput[0] := System.Char(dbLength);
  297.    _fnsAddHTabs := sInput;
  298. END;
  299. { _fnsAddHTabs }
  300.  
  301.  
  302. FUNCTION  _fnsNumToStr3(dwNum : System.Word) : STR3;
  303. {* Convert a numeric value to its string representation. *}
  304. VAR
  305.   sTemp : STR3;
  306.  
  307. BEGIN
  308.    System.Str(dwNum:3,sTemp);
  309.    _fnsNumToStr3 := sTemp;
  310. END;
  311. { _fnsNumToStr3 }
  312.  
  313.  
  314.  
  315. {*=========================== PROCEDURAL PART ==========================*}
  316.  
  317. PROCEDURE    _CopyrightDisplay;
  318. {* Outputs the copyright notice. *}
  319. BEGIN
  320.      System.WriteLn(asPurpose+'  Version '+asVersion+', '+asCopyright+'  '+asAuthor);
  321. END;  { _CopyrightDisplay }
  322.  
  323.  
  324.  
  325. PROCEDURE  _FormattedLineOutput(sOutputLine :  STRING);
  326. {* Writes formatted string. *}
  327. BEGIN
  328.     System.Write(achCR+asProgramPrompt+'Completed ('+
  329.                 _fnsNumToStr3((gddReadBytesCount*aPercent100) DIV gddInFileSize)+'%)');
  330.     System.WriteLn(gfOutputFormatText,sOutputLine);
  331. END;
  332. { _FormattedLineOutput }
  333.  
  334.  
  335. PROCEDURE  _WriteOutputLine(sOutput : STRING; bZeroFound : System.Boolean);
  336. {* Writes non-formatted string. *}
  337. VAR
  338.   chDelim       :       System.Char;
  339.   sTemp         :       STRING;
  340.  
  341. BEGIN
  342.              sTemp := sOutput;
  343.              IF (System.Length(sTemp) <> 0)
  344.                THEN  BEGIN
  345.                      IF (System.Pos(achStrDelim,sTemp) <> 0)
  346.                        THEN  chDelim := achAnotherStrDelim
  347.                        ELSE  chDelim  := achStrDelim;
  348.                      {if-then-else}
  349.                      sTemp :=  _fnsAsmPreformat +
  350.                                   chDelim +
  351.                                   sTemp +
  352.                                   chDelim;
  353.                      END;
  354.              {if-then}
  355.              IF  (bZeroFound)
  356.                THEN  BEGIN
  357.                  IF (sTemp <> asBlankStr)
  358.                        THEN  sTemp := sTemp + achDelimiter + achZero
  359.                        ELSE  sTemp := _fnsAsmPreformat + achZero;
  360.                  {if-then-else}
  361.                      END;
  362.              {if-then}
  363.              IF (gabDisplayStrOffset)
  364.                THEN  BEGIN
  365.                  sTemp := sTemp +
  366.                             _fnsAddHTabs(sOutput) +
  367.                             achSemicolon +
  368.                             achSPACE +
  369.                             achSPACE +
  370.                             achHexPrefix +
  371.                             _fnsDoubleWordToHexFmt(gddStrInFileOfs);
  372.                      END;
  373.              {if-then}
  374.              _FormattedLineOutput(sTemp);
  375. END;
  376. { _WriteOutputLine }
  377.  
  378.  
  379. PROCEDURE  _InitTemporaryValues;
  380. {* Set empty string and get current file position. *}
  381. BEGIN
  382.    gasOutAsmLine := asBlankStr;
  383.    gddStrInFileOfs := System.FilePos(gfInputByteStream);
  384. END;
  385. { _InitTemporaryValues }
  386.  
  387.  
  388. {*============================== MAIN PART =============================*}
  389.  
  390. BEGIN
  391.   _CopyrightDisplay;
  392.  
  393.      IF (System.ParamCount < 2) THEN
  394.      BEGIN
  395.           System.WriteLn(asProgramPrompt+'  help screen for you.');
  396.           System.WriteLn(' Usage: infile outfile [maxbytes [dispofs]]');
  397.           System.WriteLn('   infile   -  source filename               (def. ext. = '+asInDefExt+')');
  398.           System.WriteLn('   outfile  -  destination filename          (def. ext. = '+asOutDefExt+')');
  399.           System.WriteLn('   maxbytes -  max. number chars per line    (def. num. = ',aDefBytesPerLine,
  400.                          ', max=',aMaxBytesPerLine,')');
  401.           System.WriteLn('   dispofs  -  display file offset of string (def. = no, any string to enable)');
  402.           System.Halt(errBadParmsNumber);
  403.      END;
  404.      {if-then}
  405.  
  406.  
  407.   {** copy the parameters from command line **}
  408.     gsInFileName  := _fnsUpcaseStr(System.ParamStr(1));
  409.     gsInFileName := _fnsForceFileNameExt(gsInFileName,asInDefExt);
  410.  
  411.     gsOutFileName := _fnsUpcaseStr(System.ParamStr(2));
  412.     gsOutFileName := _fnsForceFileNameExt(gsOutFileName,asOutDefExt);
  413.  
  414.  
  415.   {* may be same names? *}
  416.     IF (gsInFileName = gsOutFileName)  THEN
  417.     BEGIN
  418.       System.WriteLn(asProgramPrompt+'Unable to use same file as input and as output');
  419.       System.Halt(errSameNames);
  420.     END;
  421.     {if-then}
  422.  
  423.  
  424.   {** source file exists? **}
  425.     IF  NOT(_fnbFileExist(gfInputStream,gsInFileName)) THEN
  426.     BEGIN
  427.       System.WriteLn(asProgramPrompt+'Unable to open file '+gsInFileName);
  428.       System.Halt(errSourceNotFound);
  429.     END;
  430.     {if-then}
  431.  
  432.  
  433.   {** destination file present? **}
  434.   IF (_fnbFileExist(gfOutputStream,gsOutFileName)) THEN
  435.   BEGIN
  436.     System.Write(asProgramPrompt+'Output file '+gsOutFileName+
  437.                  ' already exists. Overwrite? (n/y): ');
  438.     System.ReadLn(gsTempInput);
  439.     IF (System.UpCase(_fnchGetFirstChar(gsTempInput)) <> achYes)
  440.       THEN  System.Halt(errDestDontWrite);
  441.     {if-then}
  442.   END;
  443.   {if-then}
  444.  
  445.  
  446.   {** read the following parameter = bytes switch **}
  447.     IF  (System.ParamCount >= 3) THEN
  448.     BEGIN
  449.          gsTempInput := System.ParamStr(3);
  450.          System.Val(gsTempInput,gadbSymbolsInString,giErrorCode);
  451.          IF  (gadbSymbolsInString = 0)  OR
  452.               (gadbSymbolsInString > aMaxBytesPerLine)
  453.             THEN   BEGIN
  454.                System.WriteLn(asProgramPrompt+'Invalid value for BYTES switch.');
  455.                System.Halt(errBadBytesValue);
  456.                    END;
  457.          {if-then}
  458.     END;
  459.     {if-then}
  460.  
  461.  
  462.   {** display offset *}
  463.     IF  (System.ParamCount >= 4) THEN
  464.     BEGIN
  465.         gabDisplayStrOffset := System.True;
  466.     END;
  467.     {if-then}
  468.  
  469.  
  470.   {** open the source file **}
  471.     System.Assign(gfInputByteStream,gsInFileName);
  472.     {$I-}
  473.     System.Reset(gfInputByteStream);
  474.     {$I+}
  475.  
  476.     IF  (System.IoResult <> 0) THEN
  477.     BEGIN
  478.       System.WriteLn(asProgramPrompt+'Unable to open '+gsInFileName);
  479.       System.Halt(errSrcOpenFailed);
  480.     END;
  481.     {if-then}
  482.  
  483.  
  484.   {** create the destination file **}
  485.     System.Assign(gfOutputFormatText,gsOutFileName);
  486.     {$I-}
  487.     System.Rewrite(gfOutputFormatText);
  488.     {$I+}
  489.  
  490.     IF  (System.IoResult <> 0) THEN
  491.     BEGIN
  492.       System.WriteLn(asProgramPrompt+'Unable to create '+gsOutFileName);
  493.       System.Halt(errDestCreateFailed);
  494.     END;
  495.     {if-then}
  496.  
  497.  
  498.   {** get a count of bytes to read. **}
  499.     gddInFileSize := System.FileSize(gfInputByteStream);
  500.     gddByteCount  := gddInFileSize;
  501.  
  502.  
  503.   {** write first lines to output stream **}
  504.     System.WriteLn(gfOutputFormatText);
  505.     System.WriteLn(gfOutputFormatText,';  SOURCE FILE:  '+gsInFileName);
  506.     System.WriteLn(gfOutputFormatText,';  Created by '+asProgram+' utility, '+asCopyright+'  '+asAuthor);
  507.     System.WriteLn(gfOutputFormatText);
  508.     System.WriteLn(gfOutputFormatText,asAsmNoList);
  509.     System.WriteLn(gfOutputFormatText);
  510.  
  511.  
  512.   {** main loop: read_buffer/write_to_text_file **}
  513.     WHILE (gddByteCount <> 0) DO
  514.     BEGIN
  515.        System.Read(gfInputByteStream,System.Byte(gchInput));
  516.        System.Inc(gddReadBytesCount,1);
  517.        IF (gchInput = achNULL)
  518.          THEN  BEGIN
  519.              _WriteOutputLine(gasOutAsmLine,System.True);
  520.              _InitTemporaryValues;
  521.                END
  522.          ELSE  BEGIN
  523.              IF (gchInput IN setAscii8)
  524.                THEN     BEGIN
  525.                   gasOutAsmLine := gasOutAsmLine + gchInput;
  526.                   IF (System.Length(gasOutAsmLine) >= gadbSymbolsInString)
  527.                     THEN  BEGIN
  528.                      _WriteOutputLine(gasOutAsmLine,System.False);
  529.                      _InitTemporaryValues;
  530.                           END;
  531.                   {if-then}
  532.                         END
  533.                ELSE  BEGIN
  534.                  IF (gasOutAsmLine <> asBlankStr)
  535.                    THEN  _WriteOutputLine(gasOutAsmLine,System.False);
  536.                  {if-then}
  537.                  gasOutAsmLine:=  _fnsAsmPreformat +
  538.                                   _fnsByteToAsmFormat(System.Byte(gchInput));
  539.                  _FormattedLineOutput(gasOutAsmLine);
  540.                  _InitTemporaryValues;
  541.                      END;
  542.                   {if-then-else}
  543.                END;
  544.        {if-then-else}
  545.        System.Dec(gddByteCount);
  546.     END;
  547.     {while-do}
  548.  
  549.  
  550.   {** write last lines to output stream **}
  551.     IF (gasOutAsmLine <> asBlankStr)
  552.       THEN  _WriteOutputLine(gasOutAsmLine,System.False);
  553.     {if-then}
  554.     System.WriteLn(gfOutputFormatText);
  555.     System.WriteLn(gfOutputFormatText,asAsmList);
  556.  
  557.  
  558.  
  559.   {** close all files **}
  560.     System.Close(gfInputStream);
  561.     System.Close(gfOutputFormatText);
  562.  
  563.  
  564.   {** report all done **}
  565.     System.WriteLn;
  566.     System.WriteLn(asProgramPrompt+'Done.');
  567.  
  568.   {* System.Halt(errTerminateOk); *}
  569. END.
  570.